package com.seecoder.BlueWhale.serviceImpl;

import com.seecoder.BlueWhale.annotation.RoleAccessCheck;
import com.seecoder.BlueWhale.enums.RoleEnum;
import com.seecoder.BlueWhale.exception.BlueWhaleException;
import com.seecoder.BlueWhale.po.Commodity;
import com.seecoder.BlueWhale.po.Store;
import com.seecoder.BlueWhale.po.User;
import com.seecoder.BlueWhale.repository.CommodityRepository;
import com.seecoder.BlueWhale.repository.StoreRepository;
import com.seecoder.BlueWhale.service.StoreService;
import com.seecoder.BlueWhale.util.SecurityUtil;
import com.seecoder.BlueWhale.vo.StoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    StoreRepository storeRepository;
    @Autowired
    SecurityUtil securityUtil;
    @Autowired
    CommodityRepository commodityRepository;

    @Override
    @RoleAccessCheck(role = RoleEnum.MANAGER)
    public StoreVO create(StoreVO storeVO) {
        User user = securityUtil.getCurrentUser();
        Store store = storeRepository.findByName(storeVO.getName());
        if (store != null) {
            throw BlueWhaleException.storeNameAlreadyExists();
        } else if (storeVO.getLogoURL() == null) {  //必须传入一张图片链接（storeVO的logoURL类型限制了不能传超过一张图片）
            throw BlueWhaleException.notUploadStoreLogo();
        }
        Store newStore = storeVO.toPO();
        newStore.setCreateTime(new Date());
        newStore.setRating(0);
        newStore.setOwnerID(user.getId());
        storeRepository.save(newStore);
        return newStore.toVO();
    }

    @Override
    public List<StoreVO> info() {
        //用stream和map,collect转换成VO
        List<StoreVO> list =  storeRepository.findAll().stream().map(Store::toVO).collect(Collectors.toList());
        for (StoreVO storeVO:list)warpCN(storeVO);
        return list;
    }

    @Override
    public StoreVO infoSingle(int id) {
        Store store = storeRepository.findById(id);
        if (store == null) {
            throw BlueWhaleException.cannotFindStore();
        } else {
            return warpCN(store.toVO());
        }
    }

    @Override
    public boolean updateRating(int id) {
        List<Commodity> commodityList = commodityRepository.findAllByStoreId(id);
        Store store = storeRepository.findById(id);
        float rating = 0;
        int count = commodityList.size();
        for (Commodity commodity : commodityList) {
            if(commodity.getCommentNumber() == 0) {
                count --;
            } else {
                rating += commodity.getRating();
            }
        }
        rating /= count;
        store.setRating(rating);
        storeRepository.save(store);
        return true;
    }

    /**
     * 为了方便前端展示，将评论数和评分数合并到StoreVO中
     * @param storeVO 传入的StoreVO
     * @return  返回的StoreVO
     */
    private StoreVO warpCN(StoreVO storeVO){
        List<Commodity> commodityList = commodityRepository.findAllByStoreId(storeVO.getId());
        int count = 0;
        for (Commodity commodity : commodityList)count += commodity.getCommentNumber();
        storeVO.setCommentNumber(count);
        return storeVO;
    }
}
